/****** Object:  Stored Procedure [dbo].spKhoHangInsert Script Date: 6/11/2012 ******/
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[spKhoHangInsert]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
	DROP PROCEDURE [dbo].[spKhoHangInsert]
GO

/******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:					Description:
**		--------	--------				-----------------------------------
**		12/06/2012	SongTM					Innit created
*******************************************************************************/

CREATE PROCEDURE [dbo].[spKhoHangInsert]
(
	@MaKho nvarchar(5) output,
	@TenKho nvarchar(35),
	@DiaChi nvarchar(80),
	@SoDT nvarchar(15),
	@SoFax nvarchar(15),
	@Email nvarchar(30),
	@GhiChu nvarchar(100)
)
WITH ENCRYPTION

AS

SET NOCOUNT ON
if not exists (select * from [tbl_KhoHang])
	Set @MaKho=1
else
	Set @MaKho=(select RIGHT(MAX([MaKho]),3) from [tbl_KhoHang])+1
Set @MaKho='KHO'+REPLICATE('0',3-LEN(@MaKho))+@MaKho  
INSERT INTO [tbl_KhoHang]
(
	[MaKho],
	[TenKho],
	[DiaChi],
	[SoDT],
	[SoFax],
	[Email],
	[GhiChu]
)
VALUES
(
	@MaKho,
	@TenKho,
	@DiaChi,
	@SoDT,
	@SoFax,
	@Email,
	@GhiChu
)
GO

--GRANT EXECUTE ON [dbo].[spKhoHangInsert] TO [sa]
--GO
